package com.ruoyi.rsikManage.AlarmTypeDic.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.rsikManage.AlarmTypeDic.domain.TAlarmTypeDic;
import com.ruoyi.rsikManage.AlarmTypeDic.domain.TAlarmTypeDicRequest;
import com.ruoyi.rsikManage.AlarmTypeDic.service.ITAlarmTypeDicService;
import com.ruoyi.rsikManage.MonitorPoint.service.ITMonitorPointService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.core.page.TableDataInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 告警类型字典Controller
 *
 * @author sxygsj
 * @date 2024-12-17
 */
@RestController
@RequestMapping("/riskManage/dic")
public class TAlarmTypeDicController extends BaseController
{

    private final  ITAlarmTypeDicService tAlarmTypeDicService;
    private final TokenService tokenService;

    public TAlarmTypeDicController(ITAlarmTypeDicService tAlarmTypeDicService, TokenService tokenService) {
        this.tAlarmTypeDicService = tAlarmTypeDicService;
        this.tokenService = tokenService;
    }
    /**
     * 查询告警类型字典列表
     */
//@PreAuthorize("@ss.hasPermi('rsikManage:dic:list')")
    @RequestMapping(value = "/list/menu", method = {RequestMethod.GET, RequestMethod.POST})
    public TableDataInfo list(HttpServletRequest httpServletRequest,
                              @RequestBody(required = false) TAlarmTypeDicRequest tAlarmTypeDicRequest) {

        // 初始化单位代码列表
        List<Long> unitCodes = new ArrayList<>();

        // 如果请求体不为空，则从请求体中获取单位代码
        if (tAlarmTypeDicRequest != null) {
            unitCodes = tAlarmTypeDicRequest.getUnitCodes();
        }

        // 如果单位代码列表为空，则尝试从当前登录用户获取单位代码
        if (CollectionUtils.isEmpty(unitCodes)) {
            if (httpServletRequest != null && tokenService.getTokenInfo(httpServletRequest) != null) {
                unitCodes = tokenService.getLoginUserUnitCodes(httpServletRequest);
            }
        }

        // 设置最终确定的单位代码到请求参数中
        if (tAlarmTypeDicRequest == null) {
            tAlarmTypeDicRequest = new TAlarmTypeDicRequest();
        }
        tAlarmTypeDicRequest.setUnitCodes(unitCodes);

        // 调用服务层方法获取告警类型字典列表
        List<TAlarmTypeDic> list = tAlarmTypeDicService.getTAlarmTypeDicList(tAlarmTypeDicRequest);

        // 返回封装好的表格数据信息
        return getDataTable(list);
    }

    /**
     * 查询告警类型字典列表
     */
//@PreAuthorize("@ss.hasPermi('rsikManage:dic:list')")
    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    public TableDataInfo list(HttpServletRequest httpServletRequest,
                              @RequestBody(required = false) TAlarmTypeDic tAlarmTypeDic) {


        startPage();
        // 调用服务层方法获取告警类型字典列表
        List<TAlarmTypeDic> list = tAlarmTypeDicService.selectTAlarmTypeDicList( tAlarmTypeDic);

        // 返回封装好的表格数据信息
        return getDataTable(list);
    }

    /**
     * 导出告警类型字典列表
     */
    //@PreAuthorize("@ss.hasPermi('rsikManage:dic:export')")
    @Log(title = "告警类型字典", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TAlarmTypeDic tAlarmTypeDic)
    {
        List<TAlarmTypeDic> list = tAlarmTypeDicService.selectTAlarmTypeDicList(tAlarmTypeDic);
        ExcelUtil<TAlarmTypeDic> util = new ExcelUtil<TAlarmTypeDic>(TAlarmTypeDic.class);
        util.exportExcel(response, list, "告警类型字典数据");
    }

    /**
     * 获取告警类型字典详细信息
     */
    //@PreAuthorize("@ss.hasPermi('rsikManage:dic:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tAlarmTypeDicService.selectTAlarmTypeDicById(id));
    }

    /**
     * 新增告警类型字典
     */
    //@PreAuthorize("@ss.hasPermi('rsikManage:dic:add')")
    @Log(title = "告警类型字典", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TAlarmTypeDic tAlarmTypeDic)
    {
        return toAjax(tAlarmTypeDicService.insertTAlarmTypeDic(tAlarmTypeDic));
    }

    /**
     * 修改告警类型字典
     */
    //@PreAuthorize("@ss.hasPermi('rsikManage:dic:edit')")
    @Log(title = "告警类型字典", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TAlarmTypeDic tAlarmTypeDic)
    {
        return toAjax(tAlarmTypeDicService.updateTAlarmTypeDic(tAlarmTypeDic));
    }

    /**
     * 删除告警类型字典
     */
    //@PreAuthorize("@ss.hasPermi('rsikManage:dic:remove')")
    @Log(title = "告警类型字典", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tAlarmTypeDicService.deleteTAlarmTypeDicByIds(ids));
    }
}
